package com.LostAndFound.web.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.LostAndFound.domain.Article;
import com.LostAndFound.domain.FoundArticle;
import com.LostAndFound.service.OrderListForSearchLoserService;
import com.LostAndFound.utils.CommonsUtils;
import com.google.gson.Gson;

public class OrderListForSearchLoserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	OrderListForSearchLoserService service = new OrderListForSearchLoserService();
	
	// 获取列表
	public void orderListForSearchLoser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cid = request.getParameter("cid");
		if(cid==null) {
			cid = "all";
		}
		// 当前页
		int pageNumber = Integer.parseInt(request.getParameter("page"));
		// 每页总条数
		int pageSize = Integer.parseInt(request.getParameter("rows"));
		// 总记录数，且为爱心社发布的
		int total = service.getTotalToOrderForEasyUI(cid);
		List<Map<String,Object>> mapList = service.getOrderListForEasyUI(pageNumber,pageSize,cid);
		Gson gson = new Gson();
		String rows = gson.toJson(mapList);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"total\":"+total+",\"rows\":"+rows+"}");
		
	}
	// 取消认领
	public void handleCancelClaim(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aid = request.getParameter("aid");
		boolean success = service.handleCancelClaim(aid);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	// 确定认领
	public void handleConfirmClaim(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aid = request.getParameter("aid");
		String foundInfo = request.getParameter("foundInfo");
		// 封装foundArticle对象
		FoundArticle foundArticle = new FoundArticle();
		Article article = new Article();
		article.setAid(aid);
		foundArticle.setArticle(article);
		foundArticle.setFoundInfo(foundInfo);
		foundArticle.setFoundTime(CommonsUtils.getCurrentFormatTime());
		// 写入foundArticle表
		boolean success = service.handleConfirmClaim(foundArticle);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
}